package org.hospital.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by pismery on 2017-10-25.
 */
public class DateUtil {

	public static void main(String[] args) throws ParseException {
		
		//test();
		test2();
	}
	
	private static void test2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("getSeasonFirstDay(2017, 1) : "+ sdf.format(new Date(getSeasonFirstDay(2017, 1))));
		System.out.println("getSeasonFirstDay(2016, 2) : "+ sdf.format(new Date(getSeasonFirstDay(2016, 2))));
		System.out.println("getSeasonFirstDay(2016, 3) : "+ sdf.format(new Date(getSeasonFirstDay(2016, 3))));
		System.out.println("getSeasonFirstDay(2016, 4) : "+ sdf.format(new Date(getSeasonFirstDay(2016, 4))));
	}
	
	private static void test() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse("2016-2-19"));
		System.out.println("HOUR_OF_DAY : " + cal.get(Calendar.HOUR_OF_DAY));
		System.out.println("YEAR : " + cal.get(Calendar.YEAR));
		System.out.println("MONTH : " + cal.get(Calendar.MONTH));
		System.out.println("DAY_OF_WEEK : " + cal.get(Calendar.DAY_OF_WEEK));
		System.out.println("DAY_OF_MONTH : " + cal.get(Calendar.DAY_OF_MONTH));
		System.out.println("DAY_OF_YEAR : " + cal.get(Calendar.DAY_OF_YEAR));
		System.out.println("DATE : " + cal.get(Calendar.DATE));
		System.out.println("getTime : " + cal.getTime());
		System.out.println("getMinimum : " + cal.getMinimum(Calendar.DAY_OF_MONTH));
		System.out.println("getActualMaximum : " + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		System.out.println("addMonth.2017-10-31 " + addMonth("2017-10-31"));
		System.out.println("addMonth.2017-12-31 " + addMonth("2017-12-31"));
		System.out.println("addMonth.2016-1-31 " + addMonth("2016-1-31"));
		
		System.out.println("addThreeMonth.2017-10-31 " + addThreeMonth("2017-10-31"));
		System.out.println("addThreeMonth.2017-12-31 " + addThreeMonth("2017-12-31"));
		System.out.println("addThreeMonth.2016-1-31 " + addThreeMonth("2016-1-31"));

		System.out.println("minusMonth.2017-10-31 " + minusMonth("2017-10-31"));
		System.out.println("minusMonth.2017-12-31 " + minusMonth("2017-12-31"));
		System.out.println("minusMonth.2016-1-31 " + minusMonth("2016-1-31"));
		
		System.out.println("getMonthFirstDay.2016-1-5 " + getMonthFirstDay("2016-1-5"));
		System.out.println("getMonthFirstDay.2016-2-5 " + getMonthFirstDay("2016-2-5"));
		System.out.println("getMonthFirstDay.2017-2-5 " + getMonthFirstDay("2017-2-5"));
		System.out.println("getMonthFirstDay.2016-4-5 " + getMonthFirstDay("2016-4-5"));

		System.out.println("getMonthLastDay.2016-1-5 " + getMonthLastDay("2016-1-5"));
		System.out.println("getMonthLastDay.2016-2-5 " + getMonthLastDay("2016-2-5"));
		System.out.println("getMonthLastDay.2017-2-5 " + getMonthLastDay("2017-2-5"));
		System.out.println("getMonthLastDay.2016-4-5 " + getMonthLastDay("2016-4-5"));
		
		Timestamp birthdate = new Timestamp(DateUtil.getDateLong("1000-01-01 00:00:00"));
		System.out.println("birthdate(1000-01-01 00:00:00) :"+birthdate);
		System.out.println("birthdate(1000-01-01 00:00:00) :"+ new Date(birthdate.getTime()));
		
		System.out.println("now yyyy-MM-dd :"+sdf.format(new Date()));
		System.out.println("now first month yyyy-MM-dd :"+getMonthFirstDay(sdf.format(new Date())));
		
		Long l1 = getMonthFirstDayLong("2017-12-21");
		Long l2 = getMonthFirstDayLong("2017-12-11");
		System.out.println("l1==l2 : "+(l1==l2));
		System.out.println("l1.equals(l2) : "+(l1.equals(l2)));
		System.out.println("cal.getTimeInMillis() : "+cal.getTimeInMillis());
		System.out.println(DateUtil.getShortDateLong("2016-2-19"));
	}
	
	/** 
     *  
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度 
     *  
     * @param date 
     * @return 
     */ 
	public static int getSeason(Date date) {  
		  
        int season = 0;  
  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int month = c.get(Calendar.MONTH);  
        switch (month) {  
        case Calendar.JANUARY:  
        case Calendar.FEBRUARY:  
        case Calendar.MARCH:  
            season = 1;  
            break;  
        case Calendar.APRIL:  
        case Calendar.MAY:  
        case Calendar.JUNE:  
            season = 2;  
            break;  
        case Calendar.JULY:  
        case Calendar.AUGUST:  
        case Calendar.SEPTEMBER:  
            season = 3;  
            break;  
        case Calendar.OCTOBER:  
        case Calendar.NOVEMBER:  
        case Calendar.DECEMBER:  
            season = 4;  
            break;  
        default:  
            break;  
        }  
        return season;  
    }
	
	public static long getSeasonFirstDay(int year,int season) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			switch(season) {
			case 1: 
					cal.setTime(sdf.parse(year+"-01-01"));
					return cal.getTimeInMillis();
			case 2: 
				cal.setTime(sdf.parse(year+"-04-01"));
				return cal.getTimeInMillis();
			case 3: 
				cal.setTime(sdf.parse(year+"-07-01"));
				return cal.getTimeInMillis();
			case 4: 
				cal.setTime(sdf.parse(year+"-10-01"));
				return cal.getTimeInMillis();
			default:
				return -1;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
	}
	
	/**
	 * 是否是闰年
	 * 
	 * @param year
	 * @return true:闰年; false:平年;
	 */
	public static boolean isLeapYear(String year) {
		int yearInt = Integer.parseInt(year, 10);
		if ((yearInt % 4 == 0 && yearInt % 100 != 0) || (yearInt % 400 == 0 && yearInt % 100 == 0)) {
			return true;
		}
		return false;
	}

	/**
	 * 日期增加一个月
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return 
	 * 		   null date格式不对 
	 * 		   2017-10-31 --> 2017-11-30 
	 * 	  	   2017-1-31 --> 2017-02-28
	 * 	  	   2017-12-31 --> 2018-01-31
	 *         2016-1-31 --> 2016-02-29
	 */
	public static String addMonth(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		cal.add(Calendar.MONTH, 1);

		return sdf.format(cal.getTime());
	}
	
	/**
	 * 日期增加三个月
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return 
	 * 		   null date格式不对 
	 * 		   2017-10-31 --> 2018-1-31
	 * 	  	   2017-1-31 --> 2017-04-30
	 * 	  	   2017-12-31 --> 2018-3-31
	 */
	public static String addThreeMonth(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		cal.add(Calendar.MONTH, 3);
		
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 日期减一个月
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return 
	 * 		   null date格式不对 
	 * 		   2017-10-31 --> 2017-9-30 
	 * 	  	   2017-12-31 --> 2017-11-30
	 *         2016-1-31 --> 2015-12-31
	 */
	public static String minusMonth(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		cal.add(Calendar.MONTH, -1);
		
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取当月第一天的日期
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return 
	 * 		2016-1-5 --> 2016-01-01 
	 * 		2016-2-5 --> 2016-02-01 
	 * 		2017-2-5 --> 2017-02-01
	 *     	2016-4-5 --> 2016-04-01
	 */
	public static String getMonthFirstDay(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return sdf.format(cal.getTime());
	}
	/**
	 * 获取当月第一天的日期
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return 
	 * 		2016-1-5 --> 2016-01-01 
	 * 		2016-2-5 --> 2016-02-01 
	 * 		2017-2-5 --> 2017-02-01
	 *     	2016-4-5 --> 2016-04-01
	 */
	public static Long getMonthFirstDayLong(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return cal.getTime().getTime();
	}
	
	/**
	 * 获取当月第一天的日期
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return 
	 * 		2016-1-5 --> 2016-01-01 
	 * 		2016-2-5 --> 2016-02-01 
	 * 		2017-2-5 --> 2017-02-01
	 *     	2016-4-5 --> 2016-04-01
	 */
	public static Long getMonthFirstDayLong(long date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(new Date(date));
		
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return cal.getTimeInMillis();
	}
	
	/**
	 * 获取当月最后一天日期
	 * @param date	yyyy-MM-dd
	 * @return
	 * 		2016-1-5 --> 2016-01-31 
	 * 		2016-2-5 --> 2016-02-29 
	 * 		2017-2-5 --> 2017-02-28
	 *     	2016-4-5 --> 2016-04-30
	 */
	public static String getMonthLastDay(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 获取当月最后一天日期
	 * @param date	yyyy-MM-dd
	 * @return
	 * 		2016-1-5 --> 2016-01-31 
	 * 		2016-2-5 --> 2016-02-29 
	 * 		2017-2-5 --> 2017-02-28
	 *     	2016-4-5 --> 2016-04-30
	 */
	public static Long getMonthLastDayLong(long date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(date));
		
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return  cal.getTimeInMillis();
	}

	/**
	 * 获取long 的时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param time
	 * @return
	 */
	public static String getLongDate(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(time));
	}

	/**
	 * 
	 * @param time
	 *            HH:mm
	 * @return 获取long -1:转换失败
	 */
	public static long getTimeLong(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		Date d1 = new Date();
		Date d2 = new Date();
		try {
			d1 = sdf.parse(time);
			d2 = sdf.parse("00:00");
		} catch (ParseException e) {
			e.printStackTrace();
			return -1;
		}

		return d1.getTime() - d2.getTime();

	}

	/**
	 * 获取格式如1999-10-10 15:12:30的long
	 * 
	 * @param date
	 *            yyyy-MM-dd HH:mm:ss
	 * @return -1:转换失败
	 */
	public static long getDateLong(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(date).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 获取格式如1999-10-10的long
	 * 
	 * @param date
	 *            yyyy-MM-dd
	 * @return -1:转换失败
	 */
	public static long getShortDateLong(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(date).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 
	 * @return系统当前时间long
	 */
	public static long getNowLong() {
		return System.currentTimeMillis();
	}

	/**
	 * 
	 * @param date
	 *            yyyy-MM-dd HH:mm:ss
	 * @param time
	 *            HH:mm
	 * @return true:已超时间 false:未超时间
	 */
	public static boolean isTimeOut(String date, String time) {
		long now = getNowLong();
		long dateTime = getDateLong(date) + getTimeLong(time);

		if (now > dateTime) {
			return true;
		}

		return false;
	}

	/**
	 * 
	 * @param date
	 *            yyyy-MM-dd HH:mm:ss
	 * @param time
	 *            HH:mm
	 * @return true:已到时间 false:未到时间
	 */
	public static boolean isTimeUp(String date, String time) {
		long now = getNowLong();
		long dateTime = getDateLong(date) - getTimeLong(time);

		if (now > dateTime) {
			return true;
		}

		return false;
	}

	public static String getSurplusTime(String testDate, String testTime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long dateLong = DateUtil.getDateLong(testDate);
		long timeLong = DateUtil.getTimeLong(testTime);
		long nowLong = DateUtil.getNowLong();
		long surplusTime = dateLong + timeLong - nowLong;
		long time = DateUtil.getDateLong("2000-10-10 00:00:00");

		return df.format(new Date(time + surplusTime));
	}
}
